#include "McuCmd.h"
#include "Packet.h"
#include "common.h"
#include "McuManager.h"
#include "VanException.h"
#include "Utils.h"

string McuCmd::GetVersion()
{
	Packet packet(PCID_GET_VERSION);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
		return "";
	}
	
	string str((char*)pResp->GetBody(), pResp->GetBodySize());

	return str;
}

int McuCmd::GetAccState()
{
	Packet packet(PCID_ACC_GET);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
		return 0;
	}

	int value = pResp->GetBody()[0];

	return value;
}

bool McuCmd::PowerCtrl(int val)
{
	byte data = val;

	Packet packet(PCID_POWER_CTRL);
	packet.SetBody(&data, 1);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
		return false;
	}

	return true;
}

int McuCmd::GetPowerVoltage()
{
	Packet packet(PCID_GET_POWER_VOLTAGE);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
	}

	if (NULL == pResp) {
		return 0;
	}

	int value = Utils::ReadInt(pResp->GetBody(), 4);
	return value;
}

int McuCmd::GetCanCount()
{
	Packet packet(PCID_CAN_COUNT);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
	}

	if (NULL == pResp) {
		return 0;
	}

	int value = pResp->GetBody()[0];
	return value;
}

int McuCmd::GetCanSpeed(int channel)
{
	byte data = channel;
	
	Packet packet(PCID_CAN_GET_BAUDRATE);
	packet.SetBody(&data, 1);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
	}

	if (NULL == pResp) {
		return -1;
	}

	int value = Utils::ReadInt(pResp->GetBody(), 4);
	return value;
}

bool McuCmd::SetCanSpeed(int channel, int value)
{
	byte buf[5];
	buf[0] = channel;
	Utils::WriteInt(buf + 1, value, 4);

	Packet packet(PCID_CAN_SET_BAUDRATE);
	packet.SetBody(buf, 5);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
		return false;
	}

	return true;
}

bool McuCmd::CanWrite(int channel, int id, const byte* pData, int length)
{

	byte buf[14];
	memset(buf, 0, 14);

	buf[0] = channel;
	Utils::WriteInt(buf + 1, id, 4);
	buf[5] = length;

	int size = 6;
	if (NULL != pData) {
		memcpy(buf + 6, pData, length);
		size += length;
	}

	Packet packet(PCID_CAN_WRITE);
	packet.SetBody(buf, size);

	try {
		McuManager::Instance()->Request(&packet, false);
	} catch(VanException* e) {
		return false;
	}

	return true;
}

bool McuCmd::CanFilterCtrl(int channel, int type)
{
	byte buf[2];
	buf[0] = channel;
	buf[1] = type;

	Packet packet(PCID_CAN_FILTER_CTRL);
	packet.SetBody(buf, 2);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
		return false;
	}

	return true;
}

bool McuCmd::CanHwFilterAdd(int channel, uint32_t id, uint32_t mask)
{
	byte buf[9];
	buf[0] = channel;

	Utils::WriteInt(&buf[1], id, 4);
	Utils::WriteInt(&buf[5], mask, 4);

	Packet packet(PCID_CAN_HW_FILTER_ADD);
	packet.SetBody(buf, sizeof(buf));
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
		return false;
	}

	return true;
}

bool McuCmd::CanHwFilterClear(int channel)
{
	byte data = channel;
	Packet packet(PCID_CAN_HW_FILTER_CLEAR);
	packet.SetBody(&data, 1);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
		return false;
	}

	return true;
}

bool McuCmd::CanSwFilterAdd(int channel, uint32_t id, uint32_t mask)
{
	byte buf[9];
	buf[0] = channel;

	Utils::WriteInt(&buf[1], id, 4);
	Utils::WriteInt(&buf[5], mask, 4);

	Packet packet(PCID_CAN_SW_FILTER_ADD);
	packet.SetBody(buf, sizeof(buf));
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
		return false;
	}

	return true;
}

bool McuCmd::CanSwFilterClear(int channel)
{
	byte data = channel;
	Packet packet(PCID_CAN_SW_FILTER_CLEAR);
	packet.SetBody(&data, 1);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
		return false;
	}

	return true;
}

int McuCmd::GetBlockCount()
{
	Packet packet(PCID_BLOCK_COUNT);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
	}

	if (NULL == pResp) {
		return 0;
	}

	int value = pResp->GetBody()[0];
	return value;
}

bool McuCmd::BlockWrite(int index, const byte* pData, int length)
{

	byte* pBody = new byte[length + 2];
	pBody[0] = index;
	pBody[1] = length;
	memcpy(pBody+2, pData, length);

	Packet packet(PCID_BLOCK_WRITE);
	packet.SetBody(pBody, length+2);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
		return false;
	}

	return true;
}

bool McuCmd::BlockRead(int index, byte* pBuf, int length)
{
	byte body[2];
	body[0] = index;
	body[1] = length;

	Packet packet(PCID_BLOCK_READ);
	packet.SetBody(body, 2);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
	}

	if (NULL == pResp) {
		return false;
	}

	memcpy(pBuf, pResp->GetBody(), pResp->GetBodySize());
	return true;
}

int McuCmd::GetInputCount()
{
	Packet packet(PCID_INPUT_COUNT);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
	}

	if (NULL == pResp) {
		return 0;
	}

	int value = pResp->GetBody()[0];

	return value;
}

int McuCmd::InputGet(int index)
{
	byte data = index;
	Packet packet(PCID_INPUT_GET);
	packet.SetBody(&data, 1);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
	}

	if (NULL == pResp) {
		return -1;
	}

	int value = pResp->GetBody()[0];
	return value;
}

int McuCmd::GetOutputCount()
{
	Packet packet(PCID_OUTPUT_COUNT);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
	}

	if (NULL == pResp) {
		return 0;
	}

	int value = pResp->GetBody()[0];
	return value;
}

int McuCmd::OutputGet(int index)
{
	byte data = index;
	Packet packet(PCID_OUTPUT_GET);
	packet.SetBody(&data, 1);
	Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
	}

	if (NULL == pResp) {
		return -1;
	}

	int value = pResp->GetBody()[0];
	return value;
}

bool McuCmd::OutputSet(int index, int value)
{
	byte buf[2];
	buf[0] = index;
	buf[1] = value;

	Packet packet(PCID_OUTPUT_SET);
	packet.SetBody(buf, 2);
	const Packet* pResp = NULL;

	try {
		pResp = McuManager::Instance()->Request(&packet);
	} catch(VanException* e) {
		return false;
	}

	return true;
}
